layui.use(['form', 'layer', 'element', 'laydate', 'table'], function () {
    var form = layui.form;
    var layer = layui.layer;
    var element = layui.element;
    var laydate = layui.laydate;
    var table = layui.table;
    laydate.render({
        elem: '#birthDate',
        type: 'date',
        trigger: 'click',
        format: 'yyyy-MM-dd'
    });
    //调取json文件，实现省市级联
    $.get("/static/json/area.json", function (data) {
        //地址省市区级联
        var province = $("#province");
        var city = $("#city");
        var districtSelect = $("#district");
        //初始化，将省份数据赋予
        for (var i = 0; i < data.length; i++) {
            addEle(province, data[i].name);
        }
        form.render('select');

        //向select['province']中添加内容
        function addEle(ele, value) {
            var optionStr = "";
            optionStr = '<option value="' + value + '">' + value + '</option>';
            ele.append(optionStr);
        }

        //移除select['province']中所有项，赋予初始值
        function removeEle(ele) {
            ele.find("option").remove();
            var optionStr = "";
            optionStr = '<option value="0">请选择</option>';
            ele.append(optionStr);
        }

        var proText, cityText, cityItem;//被选中省市,市索引
        //选定身份后，添加当前省份数据
        form.on('select(province)', function (pro) {
            proText = pro.value;
            $.each(data, function (i, item) {
                if (proText == item.name) {
                    cityItem = i;
                    return cityItem;
                }
            });
            removeEle(city);
            removeEle(districtSelect);
            $.each(data[cityItem].city, function (i, item) {
                addEle(city, item.name);
            });
            form.render("select");
        });
        //添加县市区
        form.on('select(city)', function (c) {
            cityText = c.value;
            $.each(data, function (i, item) {
                if (cityText == item.name) {
                    cityItem = i;
                    return cityItem;
                }
            });
            removeEle(districtSelect);
            $.each(data[cityItem].city, function (i, item) {
                if (cityText == item.name) {
                    for (var j = 0; j < item.district.length; j++) {
                        addEle(districtSelect, item.district[j]);
                    }
                }
                form.render("select");
            });
        });
    });

    //接收表单数据
    form.on('submit(submit)', function (data) {
        var name = data.field.name;
        var sex = data.field.sex;
        var age = data.field.age;
        var birthDate = data.field.birthDate;
        var politicalStatus = data.field.politicalStatus;
        var phone = data.field.phone;
        var email = data.field.email;
        var address = data.field.province + data.field.city + data.field.district;
        var post = data.field.post;
        var salary = data.field.salary;
        var note = $("#note").val();
        var sData = {
            "name": name,
            "sex": sex,
            "age": age,
            "birthDate": birthDate,
            "politicalStatus": politicalStatus,
            "phone": phone,
            "email": email,
            "address": address,
            "post": post,
            "salary": salary,
            "note": note
        };
        //向后台传递数据
        $.ajax({
            type: 'post',
            url: '/staff/addStaff',
            // dataType:'json',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(sData),
            success: function (result) {
                if (result == 'success') {
                    layer.alert('添加成功！', {icon: 1}, function () {
                        location.reload();
                    });
                } else if (result == 'fail') {
                    layer.alert('添加失败，请重试！', {icon: 2});
                }
            },
            error: function (error) {
                layer.msg("error,404");
            }
        });
        return false;
    });
    //table员工信息获取
    var table1Info = table.render({
        elem: '#table1',
        url: '/staff/getStaffInfo',
        method: 'post',
        cols: [
            [
                // {field:'id',title:'ID',align:'center',width:'5%',sort:true,templet: function (d) { return d.LAY_TABLE_INDEX+1}},
                {field: 'id', title: 'ID', align: 'center', width: '5%', type: 'numbers'},
                {field: 'name', title: '姓名', align: 'center', width: '7%'},
                {field: 'sex', title: '性别', align: 'center', width: '5%'},
                {field: 'age', title: '年龄', align: 'center', width: '5%'},
                {field: 'birthDate', title: '出生日期', align: 'center', width: '9%', sort: true},
                {field: 'politicalStatus', title: '政治面貌', align: 'center', width: '8%'},
                {field: 'address', title: '地址', align: 'center', width: '11%', sort: true},
                {field: 'phone', title: '电话', align: 'center', width: '10%', sort: true},
                {field: 'email', title: '电子邮箱', align: 'center', width: '9%'},
                {field: 'post', title: '职位', align: 'center', width: '8%'},
                {field: 'salary', title: '薪水(元)', align: 'center', width: '9.5%', sort: true},
                {field: 'note', title: '备注', align: 'center', width: '5%'},
                {field: 'action1', title: '操作', align: 'center', toolbar: '#bar1', width: '8.9%'}
            ]
        ],
        page: true,

    });
    //编辑,删除表格行数据
    table.on('tool(table1)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent == "edit") {
            layer.open({
                type: 2,
                title: '修改员工信息',
                shadeClose: false,
                shade: 0.8,
                maxmin: true,
                area: ['60%', '80%'],
                content: '/staff/toEditStaffInfo?id=' + data.id
            });
        }
        if (layEvent == "del") {
            $.ajax({
                type:'post',
                url:'/staff/delStaffInfo',
                data:{
                    "id":data.id
                },
                success:function (result) {
                    if (result=="success"){
                        layer.msg("删除成功！",{icon:1,time:1000},function (index) {
                            layer.close(index);
                            location.reload();
                        });
                    }
                    if (result=="fail"){
                        layer.msg("删除失败！",{icon:2,time:1000},function (index) {
                            layer.close(index);
                        });
                    }
                    if (result=="infoError"){
                        layer.msg("删除失败！",{icon:2,time:1000},function (index) {
                            layer.close(index);
                        });
                    }
                },
                error:function (error) {
                    layer.msg("404");
                }
            });
        }
    });
    form.on('submit(edit_submit)', function (data) {
        //补充数据
        var address = data.field.province + data.field.city + data.field.district;
        var note = $("#note").val();
        data.field.address=address;
        data.field.note=note;
        $.ajax({
            type: 'post',
            url: '/staff/editStaffInfo',
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(data.field),
            success: function (result) {
                if (result=="info-error"){
                    layer.alert("上传失败！",{icon:2},function (index) {
                        layer.close(index);
                    });
                }
                if (result=="success"){
                    layer.alert("修改成功！",{icon:1},function (index) {
                        layer.close(index);
                        var pIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(pIndex);
                        parent.location.reload();
                    });
                }
                if (result=="fail"){
                    layer.alert("修改失败，请重试！",{icon:2},function (index) {
                        layer.close(index);
                    });
                }
            },
            error: function (error) {
                layer.msg("404");
            }
        });
        return false;
    });
});